<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\CompanyGongdeng as CompanyGongdengModel;
use app\common\model\CompanyGongdengOrder as CompanyGongdengOrderModel;
use app\common\model\Company as CompanyModel;
use think\Db;

/**
 * 寺院供灯控制器
 * Class Company
 * @package app\home\controller
 */
class Companygongdeng extends Base
{
    protected $moren = [
        'name' => '供灯',
        'days' => '1,3,5,7,30,90,180,360',
        'standard' =>array(
            [
                'name' => '如意灯',
                'img' => 'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E6%96%B0%E5%BF%83%E7%81%AF/%E5%A6%82%E6%84%8F%E7%81%AF%20%283%29.gif',
                'intro' => '发散能量，祈愿智慧，祛除黑恶，慈悲喜舍',
                'price' => '1'
            ],[
                'name' => '无量灯',
                'img' => 'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E6%96%B0%E5%BF%83%E7%81%AF/%E6%97%A0%E9%87%8F%E7%81%AF%20%283%29.gif',
                'intro' => '发散能量，祈愿智慧，祛除黑恶，慈悲喜舍',
                'price' => '1'
            ],[
                'name' => '觉悟灯',
                'img' => 'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E6%96%B0%E5%BF%83%E7%81%AF/%E8%A7%89%E6%82%9F%E7%81%AF%20%283%29.gif',
                'intro' => '发散能量，祈愿智慧，祛除黑恶，慈悲喜舍',
                'price' => '1'
            ],[
                'name' => '财运灯',
                'img' => 'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E6%96%B0%E5%BF%83%E7%81%AF/%E8%B4%A2%E8%BF%90%E7%81%AF%20%283%29.gif',
                'intro' => '发散能量，祈愿智慧，祛除黑恶，慈悲喜舍',
                'price' => '1'
            ],[
                'name' => '福寿灯',
                'img' => 'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E6%96%B0%E5%BF%83%E7%81%AF/%E7%A6%8F%E5%AF%BF%E7%81%AF%283%29.gif',
                'intro' => '发散能量，祈愿智慧，祛除黑恶，慈悲喜舍',
                'price' => '1'
            ],[
                'name' => '欢喜灯',
                'img' => 'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E6%96%B0%E5%BF%83%E7%81%AF/%E6%AC%A2%E5%96%9C%E7%81%AF%20%283%29.gif',
                'intro' => '发散能量，祈愿智慧，祛除黑恶，慈悲喜舍',
                'price' => '1'
            ],[
                'name' => '智慧灯',
                'img' => 'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E6%96%B0%E5%BF%83%E7%81%AF/%E6%99%BA%E6%85%A7%E7%81%AF%20%283%29.gif',
                'intro' => '发散能量，祈愿智慧，祛除黑恶，慈悲喜舍',
                'price' => '1'
            ],[
                'name' => '光明灯',
                'img' => 'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E6%96%B0%E5%BF%83%E7%81%AF/%E5%85%89%E6%98%8E%E7%81%AF%20%283%29.gif',
                'intro' => '发散能量，祈愿智慧，祛除黑恶，慈悲喜舍',
                'price' => '1'
            ],[
                'name' => '平安灯',
                'img' => 'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E6%96%B0%E5%BF%83%E7%81%AF/%E5%B9%B3%E5%AE%89%E7%81%AF%20%283%29.gif',
                'intro' => '发散能量，祈愿智慧，祛除黑恶，慈悲喜舍',
                'price' => '1'
            ]
        )
    ];
    protected $moren1 = [
        'name' => '供灯',
        'days' => '1,3,5,7,30,90,180,360',
        'standard' =>array(
            [
                'name' => '如意灯',
                'img' =>[
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E6%96%B0%E5%BF%83%E7%81%AF/%E5%A6%82%E6%84%8F%E7%81%AF-%281%29.png',
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E6%96%B0%E5%BF%83%E7%81%AF/%E5%A6%82%E6%84%8F%E7%81%AF%20%283%29.gif',
                ] ,
                'intro' => '发散能量，祈愿智慧，祛除黑恶，慈悲喜舍',
                'price' => '1'
            ],[
                'name' => '无量灯',
                'img' => [
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E6%96%B0%E5%BF%83%E7%81%AF/%E6%97%A0%E9%87%8F%E7%81%AF-%281%29.png',
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E6%96%B0%E5%BF%83%E7%81%AF/%E6%97%A0%E9%87%8F%E7%81%AF%20%283%29.gif',
                ],
                'intro' => '发散能量，祈愿智慧，祛除黑恶，慈悲喜舍',
                'price' => '1'
            ],[
                'name' => '觉悟灯',
                'img' => [
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E6%96%B0%E5%BF%83%E7%81%AF/%E8%A7%89%E6%82%9F%E7%81%AF-%281%29.png',
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E6%96%B0%E5%BF%83%E7%81%AF/%E8%A7%89%E6%82%9F%E7%81%AF%20%283%29.gif',
                ],
                'intro' => '发散能量，祈愿智慧，祛除黑恶，慈悲喜舍',
                'price' => '1'
            ],[
                'name' => '财运灯',
                'img' => [
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E6%96%B0%E5%BF%83%E7%81%AF/%E8%B4%A2%E8%BF%90%E7%81%AF-%281%29.png',
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E6%96%B0%E5%BF%83%E7%81%AF/%E8%B4%A2%E8%BF%90%E7%81%AF%20%283%29.gif',
                ],
                'intro' => '发散能量，祈愿智慧，祛除黑恶，慈悲喜舍',
                'price' => '1'
            ],[
                'name' => '福寿灯',
                'img' => [
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E6%96%B0%E5%BF%83%E7%81%AF/%E7%A6%8F%E5%AF%BF%E7%81%AF-%281%29.png',
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E6%96%B0%E5%BF%83%E7%81%AF/%E7%A6%8F%E5%AF%BF%E7%81%AF%283%29.gif',
                ],
                'intro' => '发散能量，祈愿智慧，祛除黑恶，慈悲喜舍',
                'price' => '1'
            ],[
                'name' => '欢喜灯',
                'img' => [
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E6%96%B0%E5%BF%83%E7%81%AF/%E6%AC%A2%E5%96%9C%E7%81%AF-%281%29.png',
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E6%96%B0%E5%BF%83%E7%81%AF/%E6%AC%A2%E5%96%9C%E7%81%AF%20%283%29.gif',
                ],
                'intro' => '发散能量，祈愿智慧，祛除黑恶，慈悲喜舍',
                'price' => '1'
            ],[
                'name' => '智慧灯',
                'img' => [
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E6%96%B0%E5%BF%83%E7%81%AF/%E6%99%BA%E6%85%A7%E7%81%AF-%281%29.png',
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E6%96%B0%E5%BF%83%E7%81%AF/%E6%99%BA%E6%85%A7%E7%81%AF%20%283%29.gif',
                ],
                'intro' => '发散能量，祈愿智慧，祛除黑恶，慈悲喜舍',
                'price' => '1'
            ],[
                'name' => '光明灯',
                'img' => [
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E6%96%B0%E5%BF%83%E7%81%AF/%E5%85%89%E6%98%8E%E7%81%AF-%281%29.png',
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E6%96%B0%E5%BF%83%E7%81%AF/%E5%85%89%E6%98%8E%E7%81%AF%20%283%29.gif',
                ],
                'intro' => '发散能量，祈愿智慧，祛除黑恶，慈悲喜舍',
                'price' => '1'
            ],[
                'name' => '平安灯',
                'img' => [
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E6%96%B0%E5%BF%83%E7%81%AF/%E5%B9%B3%E5%AE%89%E7%81%AF-%281%29.png',
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E6%96%B0%E5%BF%83%E7%81%AF/%E5%B9%B3%E5%AE%89%E7%81%AF%20%283%29.gif',
                ],
                'intro' => '发散能量，祈愿智慧，祛除黑恶，慈悲喜舍',
                'price' => '1'
            ]
        )
    ];
    /**
     * [getprice  获取价格]
     * @param
     * @author [C。]
     * @return json
     */
    public function getprice()
    {
        $param = input('param.');
        $model = new CompanyGongdengModel;
        $cmodel = new CompanyModel;
        try{
            $data = $model->findData($param['id'],'comid','name,days,standard');
            if(!empty($data)){
                $data['standard'] = unserialize($data['standard']);
            }else{
                $data = $this->moren;
            }
            $company = $cmodel->findData($param['id'],'id','mch_id,much_secret,wx_appid,wx_secret,is_son');
            if($company['is_son'] == 1){
                if(empty($company['mch_id']) || empty($company['wx_appid'])){
                    return admin_json(2, '暂未开通');
                }
            }else{
                if(empty($company['wx_appid']) || empty($company['wx_secret']) || empty($company['mch_id']) || empty($company['much_secret'])){
                    return admin_json(2, '暂未开通');
                }
            }
        } catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败' . $e->getMessage());
        }
        return admin_json(1, '获取成功',$data);
    }
    /**
     * [record  获取价格]
     * @param
     * @author [C。]
     * @return json
     */
    public function record()
    {
        $param = input('param.');
        $model = new CompanyGongdengOrderModel;
        $user = $this->user;
        $this->getPageAndSize($param);
        $map = [];
        try{
            if(!empty($param['type'])){
                if(!empty($param['type'])){
                    if($param['type'] == 1){
                        $code = '>';
                    }else{
                        $code = '<';
                    }
                }
                $countsql = 'select count( * ) as count from app_company_gongdeng_order where type = 1 and status = 1 and mid = '.$user['id'].' and comid = '.$param['id'].' and (paytime + day*24*3600) '.$code. time();
                $count = Db::query($countsql);
                $allpage = intval(ceil($count[0]['count'] / $this->size));//总页面
                $sql = 'select id,gongdeng,unit_price,content,day,total_price,paytime,states,content,bless,bless_img,gongdeng_img,name,to_name from app_company_gongdeng_order where type = 1 and status = 1 and mid = '.$user['id'].' and comid = '.$param['id'].' and (paytime + day*24*3600) '.$code. time().' order by paytime desc limit '.$this->from . ',' . $this->size;
                $lists = Db::query($sql);
            }else{
                if(isset($param['states'])){
                    $map['c.states'] = $param['states'];
                }
                $map['c.mid'] = $user['id'];
                $map['c.comid'] = $param['id'];
                $map['c.status'] = 1;
                $map['c.type'] = ['in','1,7'];
                $count = $model->getCount($map);//满足条件的总数
                $allpage = intval(ceil($count / $this->size));//总页数
                $lists = $model->getList($map, $this->from, $this->size,'c.id,c.gongdeng,c.content,c.day,c.total_price,c.paytime,c.states,c.content,c.bless,c.gongdeng_img,c.name,c.to_name');
            }

            foreach($lists as &$v){
                $endtime = $v['paytime'] + $v['day'] * 24 * 60 * 60;
                $v['residue'] = $v['paytime'] + $v['day']*24*3600 - time();
                $v['residue'] = time2string($v['residue']);
                if($v['residue'] < 0){
                    $v['residue'] = 0;
                }
                $v['starttime'] = date('Y-m-d H:i:s',$v['paytime']);
                $v['endtime'] = date('Y-m-d H:i:s',$endtime);
                if(empty($v['gongdeng_img'])){
                    $data = $this->moren;
                    $last_names = array_column($data['standard'], 'name');
                    $key = array_search($v['gongdeng'], $last_names);
                    $v['gongdeng_img'] = $data['standard'][$key]['img'];
                }
            }
        } catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败' . $e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [detial  获取点灯详情]
     * @param
     * @author [C。]
     * @return json
     */
    public function detial()
    {
        $param = input('param.');
        $model = new CompanyGongdengOrderModel;
        try{
            $data = $model->findData($param['id'],'id','gongdeng,states,day,name,to_name,content,paytime');
            if(!empty($data['bless_time'])){
                $data['paytime'] = date('Y-m-d',$data['bless_time']);
            }
        } catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败' . $e->getMessage());
        }
        return admin_json(1, '获取成功',$data);
    }
    /**
     * [donornum  捐款人]
     * @param
     * @author [C。]
     * @return json
     */
    public function donornum()
    {
        $param = input('param.');
        $model = new CompanyGongdengModel;
        $cmodel = new CompanyGongdengOrderModel;
        try{
            $data = $model->findData($param['id'],'comid','donor_num');
            if(empty($data)){
                $data['donor_num'] = 0;
            }
            $data['really'] = $cmodel->getAllCount(['type'=>1,'status'=>1,'comid'=>$param['id']]);
        }catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败' . $e->getMessage());
        }
        return admin_json(1, '获取成功',$data);
    }
    /**
     * [index  明细列表]
     * @param type 1添心灯 2明供灯
     * @return json
     * @author [C。]
     */
    public function index()
    {
        $param = input('param.');
        $model = new CompanyGongdengModel;
        $this->getPageAndSize($param);
        $user = $this->user;
        try{
            if($param['type'] == 1){
                $code = '>';
            }else{
                $code = '<';
            }
            $countsql = 'select count( * ) as count from app_company_gongdeng_order where type = 1 and status = 1 and mid = '.$user['id'].' and comid = '.$param['id'].' and (paytime + day*24*3600) '.$code. time();
            $count = Db::query($countsql);
            $allpage = intval(ceil($count[0]['count'] / $this->size));//总页面
            $sql = 'select id,gongdeng,name,to_name,content,day,create_time,paytime from app_company_gongdeng_order where type = 1 and status = 1 and mid = '.$user['id'].' and comid = '.$param['id'].' and (paytime + day*24*3600) '.$code. time().' order by paytime desc limit '.$this->from . ',' . $this->size;
            $lists = Db::query($sql);
            $companygongdeng = $model->findData($param['id'],'comid','standard');
            if(empty($companygongdeng)){
                $gongdeng = $this->moren;
            }else{
                $gongdeng = unserialize($companygongdeng['standard']);
            }
            $last_names = array_column($gongdeng, 'name');
            foreach($lists as &$v){
                $v['create_time'] = get_last_time($v['create_time']);
                if($param['type'] == 1){
                    $v['residue'] = $v['paytime'] + $v['day']*24*3600 - time();
                    $v['residue'] = $this->time2string($v['residue']);
                }else{
                    $v['residue'] = '已灭';
                }
                $key = array_search($v['gongdeng'], $last_names);
                $v['img'] = $gongdeng[$key]['img'];
            }
        }catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, 'error' . $e->getMessage());
        }
        return admin_json(1, 'success', [
            'count' => $count[0]['count'],
            'allpage' => $allpage,
            'lists' => $lists,
        ]);
    }
    function time2string($second){
        $day = floor($second/(3600*24));
        $second = $second%(3600*24);//除去整天之后剩余的时间
        $hour = floor($second/3600);
        $second = $second%3600;//除去整小时之后剩余的时间
        $minute = floor($second/60);
        $second = $second%60;//除去整分钟之后剩余的时间
        if($day != 0){
            $data = $day.'天'.$hour.'小时'.$minute.'分'.$second.'秒';
        }elseif($day == 0 && $hour != 0){
            $data = $hour.'小时'.$minute.'分'.$second.'秒';
        }elseif($day == 0 && $hour == 0 && $minute != 0){
            $data = $minute.'分'.$second.'秒';
        }elseif ($day == 0 && $hour == 0 && $minute == 0 && $second != 0){
            $data = $second.'秒';
        }
        //返回字符串
        return $data;
    }
    /**
     * [edit  编辑]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function edit()
    {
        $model = new CompanyGongdengModel;
        $param = input('param.');
        try{
            $gongdeng = $model->findData($param['comid'],'comid','comid');
            if(empty($gongdeng)){
                $param['standard'] = serialize($param['standard']);
                $model->addData($param);
            }else{
                $param['standard'] = serialize($param['standard']);
                $model->editDataBy($param,['comid'=>$param['comid']]);
            }
        } catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, '编辑成功');
    }
}